//
//  DetailHealthyBookViewController.m
//  棒棒哒
//
//  Created by rimi on 15/8/5.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "DetailHealthyBookViewController.h"
#import "Config.h"
#import "RGPaperLayout.h"
#import "UIImageView+WebCache.h"
#import "DetailHealthyBookCollectionViewCell.h"


#define API_KEY @"d41d8cd98f00b204e9800998ecf8427e"


@interface DetailHealthyBookViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView         *collectionView;
@property(nonatomic,strong)NSMutableArray           *dataSource;
@property(nonatomic,strong)NSMutableArray           *imageUrlArray;
@property(nonatomic,strong)UIImageView              *detailImage;
@property(nonatomic,assign)NSInteger                currentIndex;
@property(nonatomic,strong)NSMutableArray           *sortArray;
@property(nonatomic,strong)UILabel                  *bookNameLabel;


@end

@implementation DetailHealthyBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
    [self netWorkRequest];
}

-(void)initializeUserInterface
{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [BBDTabbarViewController shareTabBarController].tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"加载中..";
}

-(void)loadAfterNetWorkRequest
{
    self.title = @"健康图书";
    self.sortArray = [NSMutableArray array];
    self.imageUrlArray = [NSMutableArray array];
    for (NSDictionary *dic in _dataSource) {
        NSString *urlString = [NSString stringWithFormat:@"http://www.1ccf.com/%@",dic[@"img"]];//[@"www.1ccf.com/" stringByAppendingString:dic[@"img"] ];
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:urlString];
        [_imageUrlArray addObject:url];
    }
    
    [_imageUrlArray insertObject:[NSURL URLWithString:@"first"] atIndex:0];
    [_imageUrlArray addObject:[NSURL URLWithString:@"last"]];
    
    
    UICollectionViewFlowLayout *layOut = [[RGPaperLayout alloc]init];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-200, SCREEN_WIDTH, 200) collectionViewLayout:layOut];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"DetailHealthyBookCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CELL"];
    [self.view addSubview:_collectionView];


    _detailImage = [[UIImageView alloc]init];
    _detailImage.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT * 0.35);
    _detailImage.bounds = CGRectMake(0, 0, SCREEN_WIDTH * 0.5, SCREEN_HEIGHT * 0.4);
    [_detailImage sd_setImageWithURL:_imageUrlArray[1]];
    [self.view addSubview:_detailImage];
    
    _bookNameLabel = [CustomLabel labelWithFrame:CGRectNull Text:@"" FontSize:12 TextAlignment:NSTextAlignmentLeft TextColor:[UIColor blackColor] BackGroundColor:[UIColor whiteColor] CornerRadius:0 BorderWidth:1 BorderColor:[UIColor blackColor]];
    _bookNameLabel.bounds = CGRectMake(0, 0, SCREEN_WIDTH * 0.5, SCREEN_HEIGHT * 0.06);
    _bookNameLabel.center = CGPointMake(SCREEN_WIDTH/2, _detailImage.center.y + SCREEN_HEIGHT * 0.275 + 20);
    [self.view addSubview:_bookNameLabel];
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return  self.imageUrlArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailHealthyBookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];

    cell.imageString = _imageUrlArray[indexPath.section];



    NSLog(@"%@",_imageUrlArray);
    return cell;


}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if ([_collectionView indexPathsForVisibleItems].count == 3) {
        for (int i = 0; i <_imageUrlArray.count; i ++) {
            [_sortArray addObject:@"-100"];
        }
        for (NSIndexPath *index in [_collectionView indexPathsForVisibleItems]) {
            [_sortArray replaceObjectAtIndex:index.section withObject:[NSString stringWithFormat:@"%ld",index.section]];
        }
        [_sortArray removeObject:@"-100"];
        
        [_detailImage sd_setImageWithURL:_imageUrlArray[ [_sortArray[1] integerValue]]];
        [_sortArray removeAllObjects];
    }
    
    
    
    
    
    
    
//    NSLog(@"！！！！！！！！%@", [_collectionView indexPathsForVisibleItems]);
//    NSIndexPath *index = [_collectionView indexPathsForVisibleItems][1];
//    [_detailImage sd_setImageWithURL:_imageUrlArray[index.section]];
//    if (_currentIndex < indexPath.section) {
//        if (indexPath.section > 0) {
//            [_detailImage sd_setImageWithURL:_imageUrlArray[indexPath.section -1]];
//            _currentIndex = indexPath.section - 1 ;
//        }
//    }else if (_currentIndex > indexPath.section){
//        if (indexPath.section < _imageUrlArray.count - 1) {
//            [_detailImage sd_setImageWithURL:_imageUrlArray[indexPath.section +1]];
//            _currentIndex = indexPath.section +1 ;
//        }
//    
//    
//    }else {
//        [_detailImage sd_setImageWithURL:_imageUrlArray[indexPath.section ]];
//        _currentIndex = indexPath.section  ;
//    
//    }
    

}

-(void)netWorkRequest
{
    NSDictionary *paraDic = @{@"apikey":API_KEY,
                              @"keyword":@"养生"
                              };
    [NetRequest GET:BOOKURL parameters:paraDic success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        _dataSource = [responseObject[@"yi18"]mutableCopy];
        [self loadAfterNetWorkRequest];
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    
    
//    @try {
//        //把有可能崩溃的代码写入此处
//    }
//    @catch (NSException *exception) {
//        //如果程序崩溃，进入到这个函数
//    }
//    @finally {
//        //不管是崩溃还是不崩溃都会走
//    }
}

@end
